<template>
  <div class="login">
    <div
      id="form-main"
      :style="{
        height: height / 2 + 'px',
      }"
    >
      <H1 class="text">请先登录</H1>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="登录名" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">
            <i class="el-icon-success"></i>
            登录</el-button
          >
          <el-button @click="resetForm('ruleForm')">
            <i class="el-icon-error"></i>
            清空</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: "Login",
  data() {
    return {
      height: "",
      ruleForm: {
        name: "13641",
        password: "123",
      },
      rules: {
        name: [{ required: true, message: "请输入登录名", trigger: "change" }],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
        ],
      },
    };
  },
 // http://localhost:4568/admin_json/login
 // https://api.yxing.site/user_json/login.opera
  methods: {
    submitForm(formName) {
      var t = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios
            .post(`http://localhost:4578/admin_json/login`, {
              name: t.ruleForm.name,
              password: t.ruleForm.password,
            })
            .then((ret) => {
              if (ret.data) {
                this.$notify.success({
                  title: "成功",
                  message: "登陆成功",
                });
                this.$cookies.set("gxsf_login", true);
                this.$cookies.set("gxsf_key", ret.data);
                window.location.href = "/";
              } else {
                this.$notify.error({
                  title: "错误",
                  message: "账号密码不正确",
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  mounted() {
    this.height = window.screen.height;
  },
};
</script>
<style lang="less" scoped>
#form-main {
  width: 40%;
  background-color: #f2f6fc;
  margin-left: 27%;
  border-radius: 5px;
  box-shadow: 1px 1px 10px 1px #909399;
  margin-top: 8%;
  padding-right: 30px;
}
.text {
  font-size: 20px;
  width: 100%;
  text-align: center;
  padding: 30px 0px;
}
</style>
